from rest_framework import serializers

from recruits.models import Recruit, Enterprise


class InnerEnterpriseSerializers(serializers.ModelSerializer):

    recruits = serializers.PrimaryKeyRelatedField(label='招聘',read_only=True,many=True)

    class Meta:

        model = Enterprise

        fields = ('id','name','labels','logo','recruits','summary')


class EnterpriseRecruitsViewSerializers(serializers.ModelSerializer):

    enterprise = InnerEnterpriseSerializers(label='公司',read_only=True)

    class Meta:

        model = Recruit

        fields = ('id','jobname','salary','condition',
                  'education','type','city','createtime','enterprise','labels')



class OuterEnterpriseSerializers(serializers.ModelSerializer):

    recruits = EnterpriseRecruitsViewSerializers(label='招聘',read_only=True,many=True)
    users = serializers.StringRelatedField(label='用户',read_only=True,many=True)
    class Meta:
        model = Enterprise

        fields = '__all__'

class RecruitsViewSerializers(serializers.ModelSerializer):


    enterprise = OuterEnterpriseSerializers(label='企业',read_only=True)

    users = serializers.StringRelatedField(label='用户',read_only=True,many=True)

    class Meta:

        model = Recruit

        fields = '__all__'


class AddRecruitsVisitViewSerializers(serializers.ModelSerializer):
    '''职位访问量序列化器类'''
    id = serializers.IntegerField(label='pk')
    message = serializers.CharField(label='状态信息',default='ok',read_only=True)
    success = serializers.BooleanField(label='状态',default=True,read_only=True)

    class Meta:

        model = Recruit

        fields = ('id','success','message')


class RecruitCollectSerializers(serializers.ModelSerializer):

    success = serializers.BooleanField(label='收藏状态',default=True,read_only=True)

    message = serializers.CharField(label='状态信息',default='ok',read_only=True)

    id = serializers.CharField(label='id')

    users = serializers.CharField(label='用户',write_only=True)

    class Meta:

        model = Enterprise

        fields = ('id','success','message','users')

        extra_kwargs = {

            'users':{'required':False},
        }


class SearchJobViewEnterpriseSerializer(serializers.ModelSerializer):
    '''搜索职位关联企业嵌套序列化器模型类'''

    recruits = serializers.StringRelatedField(label='recruits',read_only=True,many=True)

    class Meta:

        model = Enterprise
        fields = ('id','name','labels','logo','recruits','summary')


class SearchJobViewSerializer(serializers.ModelSerializer):
    '''搜索职位序列化模型类'''

    enterprise = SearchJobViewEnterpriseSerializer(read_only=True)

    cityname = serializers.CharField(label='city',write_only=True,required=True)

    keyword = serializers.CharField(label='keyword',write_only=True,required=True)

    class Meta:

        model = Recruit

        exclude = ('state','detailcontent','detailrequire','visits','users','address')